<?php
require_once  __DIR__ . '/includes/authenticate.php';
include_once __DIR__ . '/db_bb_connect.php';

require_once __DIR__ . '/includes/BasketbalFunctions.php';

$basketbalfunctions = new BasketbalFunctions();

$username = $_SESSION['username'];

$sql = "SELECT firstname, lastname, position, type, user_key FROM users WHERE username = '$username'";

  if ($db->query($sql)) {  
    } else {
      echo "Error: " . $sql . "<br>" . $db->error;
    }

  $result = $db->query($sql);
  $row = $result->fetch_assoc();

  $firstname = $row['firstname']; 
  $lastname = $row['lastname']; 
  $type = $row['type'];  
  $position = $row['position'];
  $user_key = $row['user_key'];

  $sql="SELECT season
      FROM teams
      ORDER BY season DESC
      LIMIT 1";

    if ($db->query($sql)) {  
        } else {
          echo "Error: " . $sql . "<br>" . $db->error;
        }

    $result = $db->query($sql);
    $row = $result->fetch_assoc();
    $maxseason = $row['season'];
  ?>

<p>
<div class="container-fluid">

<div class="row">

<div class="col-md-12"> 

<div id="pagetitle">
  <h2>Home </h2>
  <h4><?= ucwords($position); ?> <?= $firstname; ?> <?= $lastname; ?> </h4>
</div>
  <ul class="nav nav-tabs" id="mytab">  	
  
  <li class="active"><a data-toggle="tab" href="#teamactionpoints" class="clicklog" name="teamactionpoints">Team Feedforward - Visible</a></li>
  <li><a data-toggle="tab" href="#personalactionpoints" class="clicklog" name="personalactionpoints">Personal Feedforward - Visible</a></li>
  <li><a data-toggle="tab" href="#gamestats" class="clicklog" name="gamestats">Game Stats</a></li>
  <li><a data-toggle="tab" href="#topstats" class="clicklog" name="topstats">Top Stats</a></li>
  <li><a data-toggle="tab" href="#trainingpresence" class="clicklog" name="trainingspresence">Training Presence</a></li>
  <li><a data-toggle="tab" href="#loginhistory" class="clicklog" name="activityhistory">Activity History</a></li>
  <li><a data-toggle="tab" href="#calendar" class="clicklog" name="calendar">Calendar</a></li>
  <li><a data-toggle="tab" href="#teamgoals" class="clicklog" name="teamgoals">Team Goals</a></li>
  <li><a data-toggle="tab" href="#league" class="clicklog" name="league">League</a></li>

</ul>

<div class="tab-content" id="mytabcontent">

<div id="topstats" class="tab-pane fade">
        <div id="topstats">
          <br>

<form method="POST" action="">      
        <select class="form-control" name="stattype">
          <option selected="selected" disabled="disabled">Select stats to show</option>
          <option value="DRB">DRB</option>
          <option value="ORB">ORB</option>
          <option value="TOV">TO</option>
          <option value="ST">ST</option>
          <option value="BS">BS</option>
          <option value="AST">AST</option>
          <option value="FGscored">FGI scored</option>
          <option value="FGtotal">FGI total</option>
          <option value="FG_outside_scored">FGO scored</option>
          <option value="FG_outside_total">FGO total</option>
          <option value="THPscored">3P scored</option>
          <option value="THPtotal">3P total</option>
          <option value="FTscored">FT scored</option>
          <option value="FTtotal">FT total</option>
          <option value="PF">PF</option>
      </select> 
<br>
    <div class="input-group">
        <input class="form-control" type="number" placeholder="Amount to Show" name="amounttoshow" value="5">
       <span class="input-group-addon"></span>
      </div>

      <input type="hidden" name="season" value="<?= $maxseason; ?>">

 </form>

<br><br>
 <div id="viewstatsframe"></div>
  </div>
</div>
  <div id="teamactionpoints" class="tab-pane fade in active">

<h3>Video</h3>

<ul class="nav nav-pills nav-justified">
  <li class="active"><a href="#team_videos" data-toggle="pill">Team Videos</a></li>
  <li><a href="#opponent_videos" data-toggle="pill">Opponent Videos</a></li>
</ul>


<div class="tab-content">
  
  <div id="team_videos" class="tab-pane fade in active">
<?php 

    $basketbalfunctions->ShowTeamVisibleVideoFeedforwardCurrentTeam("team"); ?>
  </div>


  <div id="opponent_videos" class="tab-pane fade">
<?php 

    $basketbalfunctions->ShowTeamVisibleVideoFeedforwardCurrentTeam("opponent"); ?>
  </div>

</div>


<h3>Other</h3>

  	
<?php 

$basketbalfunctions->ShowAttentionpointsCurrentTeam("General");

$basketbalfunctions->ShowAttentionpointsCurrentTeam("Training");

$basketbalfunctions->ShowAttentionpointsCurrentTeam("Game");

?>  
      </div>


  <div id="personalactionpoints" class="tab-pane fade">
          
    <div id="personalactionpoints"> </div>
    <?php $basketbalfunctions->ShowPersonalActionpoints(); ?> 



  </div>


  <div id="gamestats" class="tab-pane fade">
<?php 

$basketbalfunctions->LastGameStatsCurrentTeam();

$basketbalfunctions->SeasonGameStatsCurrentTeam();

?>
      </div>

  <div id="trainingpresence" class="tab-pane fade">
          <div id="presence"></div>
<?php $basketbalfunctions->LastWeekTrainingPresenceCurrentTeam(); ?>
      </div>


<div id="loginhistory" class="tab-pane fade">

 <div class="panel-group" id="accordion">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapse1">
        Login</a>
      </h4>
    </div>
    <div id="collapse1" class="panel-collapse collapse">
      <div class="panel-body"><?php $basketbalfunctions->LoginHistoryCurrentTeam(); ?></div>
    </div>
  </div>
  <div class="panel panel-default">
    <div class="panel-heading">
      <h4 class="panel-title">
        <a data-toggle="collapse" data-parent="#accordion" href="#collapse2">
        Clicked Items - Last 100 Clicks</a>
      </h4>
    </div>
    <div id="collapse2" class="panel-collapse collapse">
      <div class="panel-body"><?php $basketbalfunctions->ClickHistoryCurrentTeam(); ?></div>
    </div>
  </div>

</div> 

       
          

</div>

<div id="calendar" class="tab-pane fade">

        <div class="container-fluid">
            
            <div class="row">

              <div class="col-sm-6 col-md-6 col-lg-6">
                <h4>Upcoming Games</h4>
                <?php $basketbalfunctions->GetUpcomingGamesFromCoachID($user_key, 5); ?>

              </div>

              <div class="col-sm-6 col-md-6 col-lg-6">
                <h4>Upcoming Birthdays</h4>
                <?php   $basketbalfunctions->GetCurrentTeamBirthdaysFromCoachID($user_key, 5); ?>                
              </div>

            </div>

          </div>    
</div>

    	<div id="teamgoals" class="tab-pane">
   
          <div class="container-fluid">
            
            <div class="row">

              <div class="col-md-2 col-lg-3"></div>

              <div class="col-md-8 col-lg-6">

            	<?php 

            	$filename = $basketbalfunctions->GetLastActiveTeamGoalsFilenameFromCoachID($user_key); 

            	?>

                <img src="./uploads/teamgoals/<?= $filename ?>" alt=" " width="100%" class="img-responsive center-block" />

              </div>
              
              <div class="col-md-2 col-lg-3"></div>
 
            </div>

          </div>          
    </div>

    <div id="league" class="tab-pane">
   
          <div class="container-fluid">
            
            <div class="row">

                <div class="col-md-1 col-lg-1"></div>

                <div class="col-md-5 col-lg-5">

                  <?php 

                  $teamID = $basketbalfunctions->GetCurrentTeamIDFromCoachKey($user_key);
                  $season = $basketbalfunctions->GetLastActiveSeasonFromCoachID($user_key);

                  $basketbalfunctions->GetTeamLeagueStandings($teamID, $season);
                  
                      
                  ?>

              </div>
              <div class="col-md-5 col-lg-5">

                  <?php 

                  $basketbalfunctions->GetTeamLeagueAllResults($teamID, $season);
                      
              ?>

              </div>
              <div class="col-md-1 col-lg-1"></div>
 
            </div>

          </div>          
    </div>

</div>

</div>


</div>
</div>

<script type="text/javascript" src="./js/bootstrap-tabcollapse.js"></script>

<script type="text/javascript">

$('#mytab').tabCollapse();

$(document).on("change","#topstats select", function() {
    postData = $(this.form).serialize();
    $.ajax({
    type: "POST",
    url: "./home_viewtopstats.php",
    data: postData,
    success: function(data) {
      $("#viewstatsframe").html(data);
    },
    error: function (xhr, status, error) {
      $("#viewstatsframe").html(xhr.responseText); 
    }
    }); 
    return false;
});

$(document).on("input","#topstats input", function() {
    postData = $(this.form).serialize();
    $.ajax({
    type: "POST",
    url: "./home_viewtopstats.php",
    data: postData,
    success: function(data) {
      $("#viewstatsframe").html(data);
    },
    error: function (xhr, status, error) {
      $("#viewstatsframe").html(xhr.responseText); 
    }
    }); 
    return false;
});

$(".clicklog").on("click", function(e){
		e.preventDefault();	
		var name = $(this).attr("name");
		var username = "<?= $username ?>";
		$.ajax({
	      type: "POST",
	        url: "./clicklog.php",
	        data: { 'name' : name,
	        		'username' : username
	        },
	        success: function(data) {    
	          console.log(data);
	        },
	        error: function (xhr, status, error) {
	          console.log(xhr.responseText);
	        }
	    });

	});


</script>

